package CareerUp.SelectPoint;

import java.util.ArrayList;

public class QuickSelection {

	static int result;

	/**
	 * take array and n as parameter, return the nth big number in array
	 * 
	 * @param array
	 * @param n
	 */
	public static void quickSelec(ArrayList<Integer> array, int n) {
		int pivot = array.get(array.size() / 2);

		ArrayList<Integer> leftPart = new ArrayList<Integer>();
		ArrayList<Integer> rightPart = new ArrayList<Integer>();

		for (int i = 0; i < array.size(); i++) {
			int value = array.get(i);
			if (value >= pivot)
				rightPart.add(value);
			if (value < pivot)
				leftPart.add(value);
		}

		if (array.size() - n == leftPart.size())
			setResult(pivot);
		if (array.size() - n < leftPart.size())
			quickSelec(leftPart, n - rightPart.size());
		if (array.size() - n > leftPart.size())
			quickSelec(rightPart, rightPart.size() - ((array.size() - n) - leftPart.size()));

	}

	private static void setResult(int pivot) {
		result = pivot;
	}

	public static int getResult() {
		return result;
	}
}
